package com.h2cloud.JUCTest.TwoParseTermination;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

import static com.h2cloud.JUCTest.TimeUtil.Sleeper.sleep;

/**
 * @Package: com.h2cloud.JUCTest.TwoParseTermination
 * @ClassName: Test14
 * @Author: h2cloud
 * @CreateTime: 2022/5/29 9:58 上午
 * @Description:
 */
@Slf4j(topic = "c.Test14")
public class Test14 {
	private static void test3() {
		Thread t1 = new Thread(() -> {
			log.debug("park...");
			LockSupport.park();
			log.debug("unpark...");
			log.debug("打断状态：{}", Thread.currentThread().isInterrupted());

			// 使用 isInterrupted()相当于先get
			// 打断标记为 true，park()失效
			LockSupport.park();
			log.debug("unpark...");
		}, "t1");

		t1.start();
		sleep(1);
		t1.interrupt();
	}

	private static void test4() {
		Thread t1 = new Thread(() -> {
			log.debug("park...");
			LockSupport.park();
			log.debug("打断状态：{}", Thread.interrupted());

			// 使用 interrupted()相当于先get、再set，会清除打断标记（设置为false）
			// 打断标记为 false，park()有效
			LockSupport.park();
			log.debug("unpark...");
		});

		t1.start();
		sleep(1);
		t1.interrupt();
	}

	public static void main(String[] args) throws InterruptedException {
		test3();
		// test4();
	}
}
